package com.shelpe.services.scheduler.item.category;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

import com.shelpe.services.scheduler.po.Category;
import com.shelpe.services.scheduler.repository.CategoryRepository;


@Component("categoryReader")
@Scope("step")
public class CategoryReader implements ItemReader<List<Category> >{

	private static final Logger logger = LoggerFactory.getLogger(CategoryReader.class);
	
	@Autowired
	private CategoryRepository categoryRep;
	
	private Integer pageNo = 0;
	
	private Integer pageSize = 5;

	@Override
	public List<Category> read()
			throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
		// TODO Auto-generated method stub
		
		Pageable pager = new PageRequest(pageNo, pageSize);
		Page<Category> page = categoryRep.findAll(pager);
		List<Category> list = page.getContent();
		
		if(list != null && list.size() > 0){
			pageNo++;
			return list;
		}
		
		return null;
	}
	

}
